package bean;

import java.util.ArrayList;
import java.util.HashMap;

public class Table {
	private ArrayList<Hand> hands = new ArrayList<Hand>(4);

	private HashMap<Integer, Card> cardOnTable = new HashMap<Integer, Card>(4);
	/**
	 * 0 = spade, 1 = heart, 2 = diamond, 3 = club, 4 = notrump
	 */
	private int trump;

	/**
	 * 0 = north, 1 = east, 2 = south, 3 = west
	 */
	private int lead;

	private int turn;

	public ArrayList<Hand> getHands() {
		return hands;
	}

	public void setHands(ArrayList<Hand> hands) {
		this.hands = hands;
	}

	public int getTrump() {
		return trump;
	}

	public void setTrump(int trump) {
		this.trump = trump;
	}

	public int getLead() {
		return lead;
	}

	public void setLead(int lead) {
		this.lead = lead;
	}

	public int getHandSize() {
		return this.hands.get(turn).getSize();
	}

	public Hand getHand(int seat) {
		return hands.get(seat);
	}

	public int getTurn() {
		return turn;
	}

	public void setTurn(int turn) {
		this.turn = turn;
	}

	public int getLeadSuit() {
		return cardOnTable.get(lead).getSuit();
	}

	@SuppressWarnings("unchecked")
	public Table clone() {
		Table clonedTable = new Table();
		clonedTable.setTurn(turn);
		clonedTable.setLead(lead);
		clonedTable.setTrump(trump);
		ArrayList<Hand> cloneHands = new ArrayList<Hand>(4);
		for (Hand h : hands) {
			Hand newH = h.clone();
			cloneHands.add(newH);
		}
		clonedTable.setHands(cloneHands);
		clonedTable.setCardOnTable((HashMap<Integer, Card>) cardOnTable.clone());
		return clonedTable;
	}

	public void addCardOnTable(Integer position, Card card) {
		cardOnTable.put(position, card);
	}

	public HashMap<Integer, Card> getCardOnTable() {
		return cardOnTable;
	}

	public void setCardOnTable(HashMap<Integer, Card> cardOnTable) {
		this.cardOnTable = cardOnTable;
	}

	public void clearCardOnTable(int nextLead) {
		this.cardOnTable = new HashMap<Integer, Card>();
		lead = nextLead;
		turn = lead;
	}

	public boolean validate() {
		ArrayList<Card> cards = new ArrayList<Card>(13);
		for (Hand h : hands) {
			for (Card c : h.getCards()) {
				if (!cards.contains(c)) {
					cards.add(c);
				} else {
					return false;
				}
			}
		}
		return true;
	}

	public String toString() {
		StringBuffer index = new StringBuffer();
		for (Hand hand : hands) {
			index.append(hand.toString());
			index.append("|");
		}
		index.append(lead);
		return index.toString();
	}

	public String toEncode() {
		StringBuffer index = new StringBuffer();
		for (Hand hand : hands) {
			index.append(hand.toEncode());
			index.append("|");
		}
		index.append(lead);
		return index.toString();
	}

	public Card getWinningCard() {
		if (cardOnTable.isEmpty())
			return null;
		if (cardOnTable.size() == 1) {
			return cardOnTable.get(lead);
		}
		Card winner = null;
		for (int i = 0; i < 4; i++) {
			Card thisCard = cardOnTable.get(i);
			if (thisCard == null)
				continue;
			if (winner == null)
				winner = thisCard;
			else if (thisCard.getSuit() == winner.getSuit() && thisCard.getRank() > winner.getRank())
				winner = thisCard;
			else if (thisCard.getSuit() == trump && winner.getSuit() != trump)
				winner = thisCard;
			else if (thisCard.getSuit() == getLeadSuit() && winner.getSuit() != getLeadSuit() && winner.getSuit() != trump)
				winner = thisCard;

		}
		return winner;

	}
}
